package com.huatai.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.auth.mapper.UserMapper;
import com.huatai.auth.service.UserService;
import com.huatai.common.api.R;
import com.huatai.common.config.MinioAPI;
import com.huatai.common.constant.MinioCommonBucket;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.user.entity.User;
import com.huatai.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @program: ht-metaverse
 * @description: 用户接口
 * @author: songxiude
 * @create: 2023-08-16 14:47
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
	@Autowired
	private MinioAPI minioAPI;

	@Value("${minio.ENDPOINT}")
	private String minioUrl;
	/**
	 * 获取用户详细信息
	 *
	 * @param account
	 * @return
	 */
	public UserVo getUserInfo(String account) {
		try {
			//获取用户信息
			User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, account).eq(User::getStatus, 0));
			if (Objects.isNull(user)) return null;
			UserVo userVo = BeanUtil.copy(user, UserVo.class);
			List<String> permissions;
			//如果该用户是超级管理员获取全部权限
			if (user.getIsAdmin().intValue() == 1) {
				permissions = baseMapper.selectAdminPers();
			} else {
				//获取用户权限列表
				permissions = baseMapper.selcetUserPermissions(user.getId());
			}
			//判断用户是否上传头像
			if(StringUtil.isNotBlank(user.getAvatar())){
				//String signedUrl = minioAPI.getSignedUrl(MinioCommonBucket.HUATAI_PUB_BUCKET, user.getAvatar(), 86400);
				//userVo.setAvatar(signedUrl);
				userVo.setAvatar(minioUrl + "/" + MinioCommonBucket.HUATAI_PUB_BUCKET + "/" + user.getAvatar());

			}
			userVo.setPermissions(permissions);
			return userVo;
		}catch (Exception e){
			log.error(e.getMessage());
		}
		return null;
	}
}
